package com.yzl.frelink.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;

import com.opencsv.CSVReader;
import com.yzl.frelink.entities.Fretish;

public class CsvReader {
    public static Fretish[] readCsv(String filePath) throws Exception {
        CSVReader reader = new CSVReader(new InputStreamReader(
                new FileInputStream(filePath), StandardCharsets.UTF_8));
        List<String[]> allData = reader.readAll();
        Fretish[] fretishes = new Fretish[allData.size()];
        reader.close();

        File file = new File("./public/err/error.log");
        if (file.exists()) {
            file.delete();
        }
        
        JsonWriter.writeJsonFromCsv(filePath.replace("reqs_csv", "reqs_json").replace(".csv", ".json"), allData);
        Commander.getFretishFromJson(filePath.replace("reqs_csv", "reqs_json").replace(".csv", ".json"));
        return fretishes;
    }
}
